<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
require_once 'func.php';
abstract class Controller extends WdkController
{
	function __construct()
	{
		parent::__construct();
		$this->config = Wdk::config();
		date_default_timezone_set($this->config['timezone']);
		
		$this->session = Wdk::helper('AdminSession');
		$this->tpl = new Template_();
		$this->tpl->compile_dir = CACHE_PATH.'tpl_/_compile';
		$this->tpl->template_dir = BASE_PATH;
		$this->tpl->cache_dir = CACHE_PATH.'tpl_/_cache';
		$this->uri = Wdk::helper('Uri');
		
		require_once BASE_PATH.'admin/menus.php';
		$this->tpl->assign("shop_menu", $menu);
		$this->tpl->assign("p_menu", (isset($this->uri['uri'][0]) ? $this->uri['uri'][0] : ''));
		$this->tpl->assign("s_menu", (isset($this->uri['uri'][1]) ? $this->uri['uri'][1] : ''));
		
		$this->layout = 'default';
		$this->_init();
	}
	
	protected function _json($data=array(), $return=false)
	{
		if($return == true)
		{
			return htmlspecialchars(json_encode($data), ENT_QUOTES);
		}
		echo json_encode($data);
	}
	
	protected function _view($data = array(), $file='')
	{
		global $_SESSION;

		$this->tpl->define('view', '/admin'.$this->uri['path'].$this->uri['class'].'/'.$this->uri['method'].'.html');
		$this->tpl->assign($data);
		$this->tpl->assign('config', $this->config);
		$this->tpl->assign('session', $_SESSION);
		$this->tpl->assign('default_js', '/admin'.$this->uri['path'].$this->uri['class'].'/js/'.$this->uri['method'].'.js');
		$this->tpl->assign('default_css', '/admin'.$this->uri['path'].$this->uri['class'].'/css/'.$this->uri['method'].'.css');
		$view = $this->tpl->fetch('view');
		
		$this->tpl->define('_CONTENTS_', '/admin/layout.'.$this->layout.'.html');
		$this->tpl->assign('view', $view);
		$this->tpl->assign('PATH', '/admin'.$this->uri['path'].$this->uri['class'].'/');
		$output = $this->tpl->fetch('_CONTENTS_');
		parent::_parsing($output);
		print($output);
	}
	protected function _isLogin()
	{
		return $this->session->isLogin();
	}
	protected function _loginPage()
	{
		parent::_redirect("/admin/login/".base64_encode($_SERVER['REQUEST_URI']));
	}

	protected function _del($obj) 
	{
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data	= array('error'=>true);
		$del_seq		= (array) post('del_seq');
		/****************************************************************************************************************************/
		if(parent::_isPost())
		{
			try
			{
				if(empty($del_seq) == false)
				{
					foreach($del_seq as $seq)
					{
						$obj->load($seq);
						fb($obj);
						$obj->delete();
					}
					$assign_data['error'] = false;
				}
			}
			catch(WException $e)
			{
				$assign_data['msg'] = $e->getMessage();
			}
		}
		/*****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		exit(json_encode($assign_data));
		/****************************************************************************************************************************/		
	}
}